package com.fessc.develop.handler.processor.send;

import com.fessc.develop.entity.comvo.BankCheckFileApplyVo;
import com.fessc.develop.entity.comvo.BankCheckFileDownVo;
import com.fessc.develop.entity.dbvo.SettleTrade;
import com.fessc.develop.entity.dbvo.TPayment;
import com.fessc.develop.entity.ccbvo.CCBBankCheckJsonVo;
import com.fessc.develop.service.interfaces.ITOrgUrlManageService;
import com.fessc.develop.service.interfaces.ITPaymentService;
import com.fessc.develop.utils.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class XhshSendProcessor implements XhshISendProcessor {

    @Resource
    private ITPaymentService paymentService;

    @Resource
    private ITOrgUrlManageService tOrgUrlManageService;

    @Resource
    private OkHttpUtil okHttpUtil;

    @Transactional
    @Override
    public void prePaymentProcess(List<TPayment> tPaymentList) {
        /*
        //生成批次号
        String batchNum = DateUtil.getCurrentTimeStamp() + UUID.randomUUID().toString().substring(0,8);

        List<PrePaymentBodyVo> requestBodyList = new ArrayList<>();

        for (TPayment tPayment : tPaymentList) {
            //tPayment.setBatchNum(batchNum);
            //组装报文
            PrePaymentBodyVo bodyVo = paymentService.fakePrePaymentSendBody(tPaymentList);


            requestBodyList.add(bodyVo);
            String requestBody = JSON.toJSONString(requestBodyList);
            log.info("请求报文：" + requestBody);
            //报文加密
            //String rsaRequestBody = this.encrypt(tPayment.getOrgCode(), requestBody);

            //根据机构查询配置表获取URL,通用默认一个机构只有一个URL
            String url = tOrgUrlManageService.getOrgUrlByOrgCodeAndKey(tPayment.getOrgCode(), EnumOrgType.OTHER.toString(),null).getInterfaceUrl();

            //接口推送
            String responseBody = okHttpUtil.doPostJson(url, requestBody);
            //todo: 报文解密
            log.info("返回报文：" + responseBody);
            if (BusiUtil.isNotNull(responseBody)) {
                JSONObject head = JSON.parseObject(responseBody).getJSONObject("header");
                if (head.getInteger("code") == 200 && head.getBoolean("success")) {
                    //推送成功，更新预付款表状态为已推送SEND
                    tPayment.setPaymentStatus(EnumPaymentStatus.SEND.toString());
                }
            }
        }
        //批量更新
        paymentService.updateBatchById(tPaymentList);
        */

    }

    /**
     * 预付款交易状态查询推送
     *
     * @param tPaymentList
     */
    @Override
    public void prePaymentStatusProcess(List<TPayment> tPaymentList) {

    }

    /**
     * 跨境付汇推送处理
     *
     * @param settleTrades
     */
    @Override
    public void settleTradeProcess(List<SettleTrade> settleTrades) {

    }

    /**
     * 跨境付汇状态查询推送处理
     *
     * @param settleTrades
     */
    @Override
    public void settleTradeStatusProcess(List<SettleTrade> settleTrades) {

    }

    /**
     * 预付款退款推送处理
     *
     * @param tPaymentList
     */
    @Override
    public void prePaymentRefundProcess(List<TPayment> tPaymentList) {

    }

    /**
     * 预付款退款推送处理
     *
     * @param tPaymentList
     */
    @Override
    public void prePaymentRefundStatusProcess(List<TPayment> tPaymentList) {

    }

    /**
     * 补汇差推送
     *
     * @param tPaymentList
     */
    @Override
    public void exchangeDifProcess(List<TPayment> tPaymentList) {

    }

    /**
     * 清算交易状态推送
     *
     * @param tPaymentList
     */
    @Override
    public void prePaymentApplyProcess(List<TPayment> tPaymentList) {

    }

    /**
     * 对账推送-文件格式
     *
     * @param bankCheckFileVoList
     */
    @Override
    public void bankCheckFileApplyProcess(List<BankCheckFileApplyVo> bankCheckFileVoList) {

    }

    /**
     * 对账文件下载
     *
     * @param bankCheckFileDownVoList
     */
    @Override
    public void bankCheckFileDownLoadProcess(List<BankCheckFileDownVo> bankCheckFileDownVoList) {

    }

    /**
     * 对账推送-JSON格式
     *
     * @param ccbBankCheckJsonVoList
     */
    @Override
    public void ccbBankCheckJsonProcess(List<CCBBankCheckJsonVo> ccbBankCheckJsonVoList) {

    }

    /**
     * 获取处理器类型
     * @param
     */
    @Override
    public String getProcessorForBranch() {
        return null;
    }

}
